package com.miao.edu.authority.service.impl;


import com.miao.edu.authority.service.AuthService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author niemiao
 */
@Service
public class AuthServiceImpl implements AuthService {


    @Value("${spring.security.oauth2.jwt.signingKey}")
    private String signingKey;

    /**
     * Authorization认证开头是"bearer "
     */
    private static final String BEARER = "Bearer ";

    /**
     * 获取 jwt token 中的认证信息
     * @param jwtToken toke信息 header.payload.signature
     * @return
     */
    @Override
    public Jws<Claims> getJwt(String jwtToken) {

        // 如果 jwt 是以Bearer为开头的
        if (jwtToken.startsWith(BEARER)) {
            // 截取用户名密码那段
            jwtToken = StringUtils.substring(jwtToken,BEARER.length());
        }

        return Jwts.parser()
                .setSigningKey(signingKey)
                .parseClaimsJws(jwtToken);
    }
}
